package com.test.mylotterysystem.service.ActivityStatusChange.Operator;

import com.test.mylotterysystem.dao.DO.ActivityDO;
import com.test.mylotterysystem.dao.Mapper.ActivityMapper;
import com.test.mylotterysystem.dao.Mapper.ActivityPrizeMapper;
import com.test.mylotterysystem.service.DTO.ConvertActivityStatusDTO;
import com.test.mylotterysystem.service.serviceEnum.ActivityPrizeStatusEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class ActivityOperator extends AbstractOperator{

    @Autowired
    private ActivityMapper activityMapper;

    @Autowired
    private ActivityPrizeMapper activityPrizeMapper;

    @Override
    public Integer sequence() {
        return 2;
    }

    @Override
    public Boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        ActivityDO activityDO = activityMapper.selectActivityById(convertActivityStatusDTO.getActivityId());
        if (activityDO.getStatus().equalsIgnoreCase(convertActivityStatusDTO.getTargetUserStatus().name())) {
            return false;
        }

        int count = activityPrizeMapper.getStatusCount(activityDO.getId(), convertActivityStatusDTO.getPrizeId(), ActivityPrizeStatusEnum.INIT.name());
        if (count > 0) {
            return false;
        }
        return true;
    }

    @Override
    public Boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        try {
            activityMapper.updateStatus(convertActivityStatusDTO.getActivityId(),convertActivityStatusDTO.getTargetActivityStatus().name());
            return true;
        } catch (Exception e) {
            log.error("转换失败");
            return false;
        }
    }
}
